package cn.imvc.app.controllers;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.imvc.app.entities.Address;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.AddressModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.core.Render;

@Controller
@RequestMapping("/address")
public class AddressControllerImpl implements AddressController {

	@Autowired
	MemberModel memberModel;

	@Autowired
	AddressModel addressModel;

	@RequestMapping("/address.html")
	public String address(String token, String chooseFlag, String singleBuyFlag, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			List<Address> list = addressModel.findByMemberId(member.getId());
			model.addAttribute("addressList", list);
			model.addAttribute("chooseFlag", chooseFlag);
			model.addAttribute("singleBuyFlag", singleBuyFlag);
			return "address/address";
		} else {
			return "redirect:/member/login.html";
		}
	}
	
	@RequestMapping("/add.html")
	public String add(String token) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			return "address/add";
		} else {
			return "redirect:/member/login.html";
		}
	}
	
	@RequestMapping("/addsubmit.html")
	@ResponseBody
	public String addsubmit(String token, Address address, String area) {
		Member member = memberModel.getLoginMember(token);
		Map<String, String> result = addressModel.saveAddress(member, address, area);
		return Render.json(result);
	}
	
	@RequestMapping("/edit.html")
	public String edit(String token, Model model, int addressid) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			model.addAttribute("address", addressModel.get(addressid));
			return "address/edit";
		} else {
			return "redirect:/member/login.html";
		}
	}
	
	@RequestMapping("/editsubmit.html")
	@ResponseBody
	public String editsubmit(String token, Address address, String area) {
		Member member = memberModel.getLoginMember(token);
		Map<String, String> result = addressModel.updateAddress(member, address, area);
		return Render.json(result);
	}
	
	@RequestMapping("/del.html")
	@ResponseBody
	public String del(String token, int addressid) {
		Member member = memberModel.getLoginMember(token);
		Map<String, String> result = addressModel.delete(member, addressid);
		return Render.json(result);
	}
	
	@RequestMapping("/setDefault.html")
	@ResponseBody
	public String setDefault(String token, int addressid) {
		Member member = memberModel.getLoginMember(token);
		Map<String, String> result = addressModel.setDefault(member,addressid);
		return Render.json(result);
	}

	@RequestMapping("/address.htm")
	public String pcaddress(String token, String chooseFlag, String singleBuyFlag, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			List<Address> list = addressModel.findByMemberId(member.getId());
			model.addAttribute("addressList", list);
			model.addAttribute("chooseFlag", chooseFlag);
			model.addAttribute("singleBuyFlag", singleBuyFlag);
			return "address/pcaddress";
		} else {
			return "redirect:/member/login.htm";
		}
	}
	
	@RequestMapping("/add.htm")
	public String pcadd(String token, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			return "address/pcadd";
		} else {
			return "redirect:/member/login.htm";
		}
	}
	
	@RequestMapping("/edit.htm")
	public String pcedit(String token, Model model, int addressid) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			Address address = addressModel.get(addressid);
			model.addAttribute("address", address);
			String area = address.getProvince().getName();
			if(address.getCity() != null){
				area = area + " " + address.getCity().getName();
				if(address.getCounty() != null){
					area = area + " " + address.getCounty().getName();
				}
			}
			model.addAttribute("area", area);
			return "address/pcedit";
		} else {
			return "redirect:/member/login.htm";
		}
	}
	@RequestMapping("/orderEdit.htm")
	public String pcOrderEdit(String token, Integer addressid, Model model) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			if(addressid!=null&&addressid!=0){
				Address address = addressModel.get(addressid);
				model.addAttribute("address", address);
				String area = address.getProvince().getName();
				if(address.getCity() != null){
					area = area + " " + address.getCity().getName();
					if(address.getCounty() != null){
						area = area + " " + address.getCounty().getName();
					}
				}
				model.addAttribute("area", area);
			}
			return "address/order_address_edit";
		} else {
			return "redirect:/member/login.htm";
		}
	}
	@RequestMapping("/editsubmit.htm")
	public String pcEditsubmit(String token, Address address, String area,Model model) {
		Member member = memberModel.getLoginMember(token);
		if(address.getId()!=0){
			Map<String, String> result = addressModel.updateAddress(member, address, area);
		}else{
			Map<String, String> result1 = addressModel.saveAddress(member, address, area);
		}
		List<Address> list = addressModel.findByMemberId(member.getId());
		List<Address>  addressList = addressModel.findByMemberId(member.getId());
		model.addAttribute("addressList",addressList);
		return "/order/pccontent";
	}
	@RequestMapping("/orderSetDefaultAddress.htm")
	public String pcOrderAddressOperate(String token, int addressid ,String operateType, Model model) {
		Member member = memberModel.getLoginMember(token);
		if("del".equals(operateType)){//删除
			Map<String, String> result = addressModel.delete(member, addressid);
		}else if ("setDefault".equals(operateType)){//设置默认
			Map<String, String> result = addressModel.setDefault(member,addressid);
		}
		List<Address>  addressList = addressModel.findByMemberId(member.getId());
		model.addAttribute("addressList",addressList);
		return "/order/pccontent";
	}

}
